package cn.raven.basic.interceper;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.basic.jwt.JwtUtils;
import cn.raven.basic.jwt.Payload;
import cn.raven.basic.jwt.RsaUtils;
import cn.raven.basic.jwt.UserJwt;
import cn.raven.system.domain.Permission;
import cn.raven.system.mapper.PermissionMapper;
import cn.raven.user.domain.Logininfo;
import cn.raven.user.domain.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.security.PublicKey;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class LoginIntercrper implements HandlerInterceptor {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PermissionMapper permissionMapper;
    @Value("${jwt.rsa.pub}")
    private String jwtRsaPub;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从请求头获取token
        String token = request.getHeader("token");
        // 3.1.设置编码字符集
        response.setCharacterEncoding("UTF-8");
        // 3.2.设置响应参数类型为JSON
        response.setContentType("application/json;charset=UTF-8");
        //判断token是否存在
        if (token != null){
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource(jwtRsaPub).getFile());
            Payload<UserJwt> payload = JwtUtils.getInfoFromToken(token, publicKey, UserJwt.class);
            if (payload != null){
                UserJwt userInfo = payload.getUserInfo();
                Logininfo logininfo = userInfo.getLogininfo();
                //判断是员工来访问
                if(logininfo.getType() == 0){
                    //得到本方法的对象
                    HandlerMethod handlerMethod = (HandlerMethod) handler;
                    //判断方法上自定义注解的字节码对象
                    PreAuthorize preAuthorize = handlerMethod.getMethodAnnotation(PreAuthorize.class);
                    //判断是否有注解，没有就是公共资源
                    if(preAuthorize != null){
                        String sn = preAuthorize.sn();
                        if (!userInfo.getPermissions().contains(sn)){
                            PrintWriter writer = response.getWriter();
                            writer.write("{\"success\":\"false\",\"message\":\"noPermission\"}");
                            writer.flush();
                            writer.close();
                        }
                    }
                }
                return true;
            }
        }
        PrintWriter writer = response.getWriter();
        writer.write("{\"success\":\"false\",\"message\":\"noLogin\"}");
        writer.flush();
        writer.close();
        return false;

    }
}
